package h2db.function.ext;

import java.net.InetAddress;
import java.net.UnknownHostException;
import java.text.ParseException;
import java.text.SimpleDateFormat;
import java.util.Date;
import java.util.UUID;
public class H2DBFunctionExt {
	/**
	 * 用法：SELECT uuid(); H2数据库注册uuid函数：CREATE ALIAS uuid FOR
	 * "h2db.function.ext.H2DBFunctionExt.uuid";
	 *
	 * @Method: uuid
	 * @Description: 实现MySQL数据库的uuid函数，用于生成UUID
	 *
	 * @return
	 */
	public static String uuid() {
		return UUID.randomUUID().toString();
	}

	/**
	 * H2数据库注册currentTime函数：CREATE ALIAS IF NOT EXISTS currentTime FOR
	 * "h2db.function.ext.H2DBFunctionExt.now";
	 *
	 * @Method: now
	 * @Description: 实现MySQL数据库的now()函数，用于生成当前系统时间
	 *
	 * @return
	 */
	public static String now() {
		return new Date().toLocaleString();
	}

	/**
	 * H2数据库注册IP函数：CREATE ALIAS IF NOT EXISTS IP FOR
	 * "h2db.function.ext.H2DBFunctionExt.getIp";
	 *
	 * @Method: getIp
	 * @Description:
	 *
	 * @return
	 */
	public static String getIp() {
		try {
			InetAddress addr = InetAddress.getLocalHost();
			// 获得本机IP
			return addr.getHostAddress();
		} catch (UnknownHostException e) {
			e.printStackTrace();
			return "未知的IP地址";
		}
	}

	/**
	 * H2数据库注册date_format函数：CREATE ALIAS IF NOT EXISTS date_format FOR
	 * "h2db.function.ext.H2DBFunctionExt.date_format";
	 *
	 * @Method: date_format
	 * @Description: 实现MySQL数据库的date_format()函数，用于格式化日期
	 * @param date
	 * @param pattern
	 * @return
	 */
	public static String DATE_format(String date, String pattern) {
		if (date != null) {
			pattern = transfer(pattern);
			SimpleDateFormat sdf = new SimpleDateFormat(pattern);
			try {
				Date temp = sdf.parse(date);
				return sdf.format(temp);
			} catch (ParseException e) {
				e.printStackTrace();
			}
		}
		return "";
	}

	private static String transfer(String pattern) {
		switch (pattern) {
			case "%Y-%m-%d":
				return "yyyy-MM-dd";
		}
		return "yyyy-MM-dd HH:mm:ss";
	}

	public static void main(String[] args) {
		System.out.println(DATE_format("2017-09-22 19:35:40", "%Y-%m-%d"));
	}
}